import { DatePicker, Input, InputNumber, Select } from '@/components/AntdProxy';
import CommonDrawer from '@/components/CommonDrawer';
import { CrudFormLayout } from '@/components/Crud';
import { VehicleSelect } from '@/components/CustomForm';
import { BATTERY_TYPE_OPTIONS } from '@/constants/enums';
import { REQUIRED } from '@/constants/rules';
import request from '@/utils/request';
import { useAsyncEffect } from 'ahooks';
import { Form, message } from 'antd';
import dayjs from 'dayjs';
const Save = (props) => {
  const { open, record, onCancel, onOk } = props;
  const [form] = Form.useForm();
  useAsyncEffect(() => {
    if (open && record) {
      form.setFieldsValue({
        ...record,
        firstUseDate: record.firstUseDate ? dayjs(record.firstUseDate) : null,
      });
    }
  }, [open, record]);

  const handleOk = async () => {
    const values = await form.validateFields();
    console.log('values', values);
    const url = record ? '/datas/battery/update' : '/datas/battery/add';
    const params = {
      ...values,
      firstUseDate: values.firstUseDate
        ? values.firstUseDate.format('YYYY-MM-DD')
        : null,
    };
    if (record) {
      params.batteryId = record?.batteryId;
    }
    await request({
      url,
      method: 'POST',
      data: params,
    });
    message.success('操作成功');
    onOk?.();
  };

  return (
    <CommonDrawer
      title={record ? '编辑' : '新建'}
      open={open}
      onClose={onCancel}
      onOk={handleOk}
      afterOpenChange={(visible) => {
        if (!visible) {
          form.resetFields();
        }
      }}
      width={1000}
      destroyOnHidden
    >
      <Form form={form} layout="vertical">
        <CrudFormLayout itemMinWidth={250}>
          <Form.Item label="车牌号" name="vehicleId">
            <VehicleSelect />
          </Form.Item>
          <Form.Item label="BMS编码" name="bmsCode" rules={[REQUIRED]}>
            <Input placeholder="请输入BMS编码" />
          </Form.Item>
          <Form.Item label="类型" name="type" rules={[REQUIRED]}>
            <Select placeholder="请选择类型" options={BATTERY_TYPE_OPTIONS} />
          </Form.Item>
          <Form.Item label="额定容量(Ah)" name="ratedCapacity">
            <InputNumber
              placeholder="请输入额定容量"
              style={{ width: '100%' }}
            />
          </Form.Item>
          <Form.Item label="额定能量(kWh)" name="ratedEnergy">
            <InputNumber
              placeholder="请输入额定能量"
              style={{ width: '100%' }}
            />
          </Form.Item>
          <Form.Item label="生产厂商" name="mader">
            <Input placeholder="请输入生产厂商" />
          </Form.Item>
          <Form.Item label="电芯数" name="batteryCells">
            <InputNumber placeholder="请输入电芯数" style={{ width: '100%' }} />
          </Form.Item>
          <Form.Item label="温度探针数" name="temperatureProbes">
            <InputNumber
              placeholder="请输入温度探针数"
              style={{ width: '100%' }}
            />
          </Form.Item>
          <Form.Item label="首次使用日期" name="firstUseDate">
            <DatePicker
              placeholder="请选择首次使用日期"
              style={{ width: '100%' }}
            />
          </Form.Item>
          <Form.Item label="SOC(%)" name="soc">
            <InputNumber placeholder="请输入SOC" style={{ width: '100%' }} />
          </Form.Item>
          <Form.Item label="SOH(%)" name="soh">
            <InputNumber placeholder="请输入SOH" style={{ width: '100%' }} />
          </Form.Item>
          <Form.Item label="总电压(V)" name="voltages">
            <InputNumber placeholder="请输入总电压" style={{ width: '100%' }} />
          </Form.Item>
          <Form.Item label="总电流(A)" name="amperages">
            <InputNumber placeholder="请输入总电流" style={{ width: '100%' }} />
          </Form.Item>
          <Form.Item label="累计充电量(kWh)" name="totalChargeValue">
            <InputNumber
              placeholder="请输入累计充电量"
              style={{ width: '100%' }}
            />
          </Form.Item>
          <Form.Item label="累计放电量(kWh)" name="totalDischargeValue">
            <InputNumber
              placeholder="请输入累计放电量"
              style={{ width: '100%' }}
            />
          </Form.Item>
          <Form.Item label="冷却方式" name="coolType">
            <Input placeholder="请输入冷却方式" />
          </Form.Item>
          <Form.Item label="纬度" name="lat">
            <InputNumber placeholder="请输入纬度" style={{ width: '100%' }} />
          </Form.Item>
          <Form.Item label="经度" name="lng">
            <InputNumber placeholder="请输入经度" style={{ width: '100%' }} />
          </Form.Item>
        </CrudFormLayout>
        <Form.Item label="备注" name="remark">
          <Input.TextArea maxLength={150} />
        </Form.Item>
      </Form>
    </CommonDrawer>
  );
};

export default Save;
